package org.BiteTheDice;

import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
//import android.os.CountDownTimer;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import graphics.*;
import org.BiteTheDice.R;
public class RunGame extends Activity {

	Surface surface;
	private static int MAX_NUM_PLAYERS = 2;
	//private static long TIME=123000;
	static int numOfPlayers = 1;
	//protected static boolean runningWindow=false;
	//private static boolean fromBack=false;
	private String winner="Player 1";
	//private CountDownTimer timer=null;

	private SoundPool soundPool;
	private int goodSoundID;
	private int badSoundID;
	private int spacielSoundID;
	boolean loaded = false;

	public boolean playEffect(int type) {
		if (!loaded) return false;
		// Getting the user sound settings
		AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
		float actualVolume = (float) audioManager
		.getStreamVolume(AudioManager.STREAM_MUSIC);
		float maxVolume = (float) audioManager
		.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		float volume = Math.min(1f,actualVolume/maxVolume+0.4f);
		Log.d("volume","actual " +actualVolume+" max "+maxVolume+" ratio: "+volume);
		// Is the sound loaded already?
		if (type==0) soundPool.play(goodSoundID, volume, volume, 1, 0, 1f);
		else if (type==1)soundPool.play(badSoundID, volume, volume, 1, 0, 1f);
		else soundPool.play(spacielSoundID, volume, volume, 1, 0, 1f);
		return true;
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
		// Load the sound
		if (Prefs.getEffects(this)){
			soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
			soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
				@Override
				public void onLoadComplete(SoundPool soundPool, int sampleId,
						int status) {
					loaded = true;
				}
			});
			goodSoundID = soundPool.load(this, R.raw.goodsound, 1);
			badSoundID = soundPool.load(this, R.raw.badsound, 1);
			spacielSoundID = soundPool.load(this, R.raw.spaciel, 1);
		}
//		runningWindow=false;
	//	fromBack=false;
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		surface = new Surface(this,Math.min(numOfPlayers, MAX_NUM_PLAYERS));
		setContentView(surface);
		//setTimer(TIME);
	}

	/*private void setTimer(long millisUntilFinished){
		timer=new CountDownTimer(millisUntilFinished, 1000) {    
			public void onTick(long millisUntilFinished) {
			}  
			public void onFinish() {  
				if (numOfPlayers==1){
					BiteTheDice.currentScore=surface.getScore()[0];
					runningWindow=true;
					Intent j = new android.content.Intent(RunGame.this, GameEndsActivity.class);
					startActivity(j);
				}
				else{
					int score1=surface.getScore()[0];
					int score2=surface.getScore()[1];
					if (score1>score2)
						winner="Player 1";
					else if (score2>score1)
						winner="Player 2";
					else 	
						winner="It's a tie!";
					BiteTheDice.currentWinner=winner;
					runningWindow=true;
					Intent j = new Intent(RunGame.this, GameEndsMultiPlayer.class);
					startActivity(j);
				}
			}
		}.start(); 
	}
	*/
	
	public void startEndGameMenu(int player1Score, int player2Score){
		if (numOfPlayers==1){
			BiteTheDice.currentScore=player1Score;
			//runningWindow=true;
			Intent j = new android.content.Intent(RunGame.this, GameEndsActivity.class);
			startActivity(j);
		}
		else{
			if (player1Score>player2Score)
				winner="Player 1";
			else if (player2Score>player1Score)
				winner="Player 2";
			else 	
				winner="It's a tie!";
			BiteTheDice.currentWinner=winner;
			//runningWindow=true;
			Intent j = new Intent(RunGame.this, GameEndsMultiPlayer.class);
			startActivity(j);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		Music.stop(this);
		/*timer.cancel();
		if (!fromBack && !runningWindow){
			//timer.onFinish();
		}
		else{
			fromBack=false;
			runningWindow=false;
		}*/
		surface.onPause();	
	}

	@Override
	protected void onResume() {
		super.onResume();
		Music.play(this, R.raw.gamemusic);
		surface.onResume();
	}
	public static void setNumPlayer(int num){
		numOfPlayers=num;
	}
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
			//timer.cancel();
			//fromBack=true;
			surface.onPause();
			Intent j = new Intent(this, BiteTheDice.class);
			startActivity(j);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}